# -*- coding: utf-8 -*-

from odoo import models, fields, api


class ActivityRegistrationItem(models.Model):
    _name = 'activity.registration.item'
    _description = '会议报名项目'
    _order = 'sequence, id'
    
    name = fields.Char(string='项目名称', required=True)
    sequence = fields.Float(string='排序', default=10, digits=(10, 2))  # 支持小数，用于插入操作
    activity_id = fields.Many2one('speech.activity', string='关联会议', required=True, ondelete='cascade')
    description = fields.Text(string='项目描述')
    
    # 角色分类
    role_category_id = fields.Many2one('item.role.category', string='角色分类', required=False, ondelete='restrict', 
                                        help='角色分类，如未指定则为未分类')
    role_category_name = fields.Char(related='role_category_id.name', string='分类名称', store=True)
    
    # 报名限制
    max_registrations = fields.Integer(string='最大报名人数', default=1, help='0表示不限制')
    
    # 必填项设置
    require_introduction = fields.Boolean(string='自我介绍必填', default=True)
    require_photo = fields.Boolean(string='个人照片必填', default=True)
    require_speech_intro = fields.Boolean(string='演讲介绍必填', default=True)
    
    # 报名记录
    item_registration_ids = fields.One2many('activity.item.registration', 'item_id', string='报名记录')
    registration_count = fields.Integer(string='报名人数', compute='_compute_registration_count', store=True)
    
    @api.depends('item_registration_ids')
    def _compute_registration_count(self):
        for record in self:
            record.registration_count = len(record.item_registration_ids)
    
    @api.model
    def create_default_items(self, activity_id, template_type='chinese'):
        """为会议创建默认的报名项目
        
        Args:
            activity_id: 会议ID
            template_type: 模板类型 'chinese' 或 'english'
        """
        # 获取角色分类
        category_meeting = self.env['item.role.category'].search([('name', '=', '会议角色')], limit=1)
        category_prepared = self.env['item.role.category'].search([('name', '=', '备稿演讲')], limit=1)
        category_impromptu = self.env['item.role.category'].search([('name', '=', '即兴演讲')], limit=1)
        category_evaluation = self.env['item.role.category'].search([('name', '=', '点评')], limit=1)
        
        # 如果分类不存在，创建它们
        if not category_meeting:
            category_meeting = self.env['item.role.category'].create({'name': '会议角色', 'sequence': 10})
        if not category_prepared:
            category_prepared = self.env['item.role.category'].create({'name': '备稿演讲', 'sequence': 20})
        if not category_impromptu:
            category_impromptu = self.env['item.role.category'].create({'name': '即兴演讲', 'sequence': 30})
        if not category_evaluation:
            category_evaluation = self.env['item.role.category'].create({'name': '点评', 'sequence': 40})
        
        if template_type == 'english':
            default_items = [
                # (sequence, name, category_id)
                (1, 'Host', category_meeting.id),
                (2, 'Club Introduction', category_meeting.id),
                (3, 'Timer', category_meeting.id),
                (4, 'Ah counter', category_meeting.id),
                (5, 'Grammarian', category_meeting.id),
                (6, 'General Evaluator', category_meeting.id),
                (7, 'Joker', category_meeting.id),
                (8, 'Speaker 1', category_prepared.id),
                (9, 'Speaker 2', category_prepared.id),
                (10, 'Speaker 3', category_prepared.id),
                (11, 'Evaluator 1', category_evaluation.id),
                (12, 'Evaluator 2', category_evaluation.id),
                (13, 'Evaluator 3', category_evaluation.id),
                (14, 'Table topic master', category_meeting.id),
                (15, 'Table topic Evaluator', category_evaluation.id),
                (16, 'Impromptu 1', category_impromptu.id),
                (17, 'Impromptu 2', category_impromptu.id),
                (18, 'Impromptu 3', category_impromptu.id),
                (19, 'Impromptu 4', category_impromptu.id),
                (20, 'Impromptu 5', category_impromptu.id),
                (21, 'Impromptu 6', category_impromptu.id),
                (22, 'Admission speech', category_prepared.id),
            ]
        else:  # chinese
            default_items = [
                # (sequence, name, category_id)
                (1, '整场主持人', category_meeting.id),
                (2, '俱乐部介绍', category_meeting.id),
                (3, '计时官', category_meeting.id),
                (4, '哼哈官', category_meeting.id),
                (5, '语言官', category_meeting.id),
                (6, '总评官', category_meeting.id),
                (7, '开心大使', category_meeting.id),
                (8, '备稿分享一', category_prepared.id),
                (9, '备稿分享二', category_prepared.id),
                (10, '备稿分享三', category_prepared.id),
                (11, '备稿点评一', category_evaluation.id),
                (12, '备稿点评二', category_evaluation.id),
                (13, '备稿点评三', category_evaluation.id),
                (14, '即兴主持', category_meeting.id),
                (15, '即兴点评', category_evaluation.id),
                (16, '即兴演讲一', category_impromptu.id),
                (17, '即兴演讲二', category_impromptu.id),
                (18, '即兴演讲三', category_impromptu.id),
                (19, '即兴演讲四', category_impromptu.id),
                (20, '即兴演讲五', category_impromptu.id),
                (21, '即兴演讲六', category_impromptu.id),
                (22, '入会演讲', category_prepared.id),
            ]
        
        items = []
        for sequence, name, category_id in default_items:
            # 根据角色分类设置默认必填项
            # 备稿演讲：三个都必填
            # 即兴演讲：个人照片必填、演讲介绍必填（不需要自我介绍）
            # 其他角色：自我介绍必填、个人照片必填（不需要演讲介绍）
            if category_id == category_prepared.id:
                # 备稿演讲
                require_intro = True
                require_photo = True
                require_speech = True
            elif category_id == category_impromptu.id:
                # 即兴演讲
                require_intro = False
                require_photo = True
                require_speech = True
            else:
                # 其他角色（会议角色、点评）
                require_intro = True
                require_photo = True
                require_speech = False
            
            item = self.create({
                'name': name,
                'sequence': sequence,
                'activity_id': activity_id,
                'role_category_id': category_id,
                'require_introduction': require_intro,
                'require_photo': require_photo,
                'require_speech_intro': require_speech,
            })
            items.append(item)
        
        return items


class ActivityItemRegistration(models.Model):
    _name = 'activity.item.registration'
    _description = '项目报名记录'
    _order = 'create_date desc'
    
    item_id = fields.Many2one('activity.registration.item', string='报名项目', required=True, ondelete='cascade')
    activity_id = fields.Many2one(related='item_id.activity_id', string='关联会议', store=True)
    user_id = fields.Many2one('wechat.user', string='报名用户', required=True)
    
    # 报名信息
    name = fields.Char(string='我是谁', required=True)
    introduction = fields.Text(string='自我介绍')
    photo_url = fields.Char(string='个人照片URL')
    speech_intro = fields.Text(string='演讲介绍')
    
    # 演讲风采照片（支持多张，逗号分隔）
    performance_photo_urls = fields.Text(string='演讲风采照片URLs', help='管理员上传的演讲过程照片，多张用逗号分隔')
    
    status = fields.Selection([
        ('pending', '待审核'),
        ('approved', '已通过'),
        ('rejected', '已拒绝'),
    ], string='状态', default='approved', required=True)  # 默认改为approved
    
    create_date = fields.Datetime(string='报名时间', readonly=True)
    
    _sql_constraints = [
        ('unique_user_item', 'unique(item_id, user_id)', '您已经报名过此项目！'),
    ]

